using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace SchemaProvider
{
    public abstract class ViewCodeProviderBase : IViewCodeProvider
    {

        /// <summary>
        /// Converts a column name to a field name. The derived class must implement it.
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public string ToFieldName(string columnName)
        {
            return "_" + CodeUtility.PascalCaseToCamelCase(ToPropertyName(columnName));
        }

        /// <summary>
        /// Converts a column name to a property name. The derived class must implement it.
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public abstract string ToPropertyName(string columnName);


        #region ICodeProvider Members

        public virtual string MapClassAttribute(string name)
        {
            return "[Table(TableName = \"" + name + "\")]";
        }

        public abstract string MapClassName(string name);


        public virtual string MapBaseClassName(string name)
        {
            string baseClassName = "ViewGateway";
            return baseClassName + "<" + this.MapClassName(name) + ">";
        }

        #endregion

        #region IViewCodeProvider Members

        public virtual string MapMemberVariablesBlock(DataColumnCollection columns)
        {
            StringBuilder content = new StringBuilder();
            if (columns.Count <= 0)
            {
                return content.ToString();
            }
            content.Append("\n");
            foreach (DataColumn column in columns)
            {
                content.Append(CodeUtility.CreateTabSpaces(2) + "private " + CodeUtility.GetDataType(column) + " " + this.ToFieldName(column.ColumnName) + ";\n");
            }
            return content.ToString();
        }

        public virtual string MapPropertiesBlock(DataColumnCollection columns, bool needGateway)
        {
            StringBuilder content = new StringBuilder();
            if (columns.Count <= 0)
            {
                return content.ToString();
            }

            content.Append("\n");
            foreach (DataColumn column in columns)
            {
                //attribute statement
                if (needGateway)
                {
                    content.Append(CodeUtility.CreateTabSpaces(2));
                    content.Append("[Column(ColumnName = \"" + column.ColumnName + "\")]\n");
                }

                //property block
                string propertyName = this.ToPropertyName(column.ColumnName);
                string fieldName = this.ToFieldName(column.ColumnName);

                content.Append(CodeUtility.CreateTabSpaces(2) + "public " + CodeUtility.GetDataType(column) + " " + propertyName + "\n");
                content.Append(CodeUtility.CreateTabSpaces(2) + "{\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "get\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "{\n");
                content.Append(CodeUtility.CreateTabSpaces(4) + "return " + fieldName + ";\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "}\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "set\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "{\n");
                content.Append(CodeUtility.CreateTabSpaces(4) + fieldName + "=value;\n");
                content.Append(CodeUtility.CreateTabSpaces(3) + "}\n");
                content.Append(CodeUtility.CreateTabSpaces(2) + "}\n\r");
            }

            return content.ToString();
        }

        #endregion
    }
}
